package Test.Login_teacher.application;

import Test.Login_teacher.dao.UserDao;
import Test.Login_teacher.dao.impl.UserDaoImpl;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class server {
    //服务器的登录功能
    public void login() throws IOException {
        //创建服务器
        ServerSocket socket = new ServerSocket(8088);
        //
        UserDao dao = new UserDaoImpl();
        while (true){
            Socket client = socket.accept();
            //获取输入输出流
            BufferedReader bw = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8));
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(client.getOutputStream(),StandardCharsets.UTF_8));
            //接收客户端传过来的账号和密码
            String userInfo = bw.readLine();
            String []user = userInfo.split("#");
            String userName = user[0];
            String password = user[1];
            boolean isLogin = dao.login(userName,password);
            if(isLogin){
                //发送数据给客户端
                pw.println("登录成功"+userName+password);
                pw.flush();
            }else {
                pw.println("登录失败");
                pw.flush();
            }
            //关闭流
            pw.close();
            bw.close();
            client.close();
        }
    }
    //服务器的注册功能
}

